package com.cn.aam.checaiduo.ui.me.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.baoyz.widget.PullRefreshLayout;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.CommandV2;
import com.cn.aam.checaiduo.base.BaseFragment;
import com.cn.aam.checaiduo.base.adapter.BaseRecycerAdapter;
import com.cn.aam.checaiduo.base.adapter.decoration.GridSpacingItemDecoration;
import com.cn.aam.checaiduo.base.adapter.delegator.RecyclerViewDelegatorV2;
import com.cn.aam.checaiduo.bean.me.InvestRecordB;
import com.cn.aam.checaiduo.ui.me.InvestRecordDetailActivity;
import com.cn.aam.checaiduo.ui.me.adapter.InvestRecordAdapter;
import com.cn.aam.checaiduo.ui.me.command.InvestRecordCommand;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.cn.aam.checaiduo.utils.WindowsUtils;
import com.cn.aam.checaiduo.widget.MultiStateView;

import butterknife.BindView;


/**
 * 投资记录
 */
public class InvestRecordFragment extends BaseFragment implements RecyclerViewDelegatorV2.OnLoadFirstPageLister {

    //    1：众筹中，2：销售中，3：已回款
    public static final String INVEST_COLLECT = "1";//众筹中
    public static final String INVEST_ONSALE = "2";//起息中
    public static final String INVEST_PAYMENT = "3";//已回款


    InvestRecordCommand command;

    private RecyclerViewDelegatorV2<InvestRecordB> mDelegator;

    @BindView(R.id.id_recyclerview)
    RecyclerView mRecycler;
    @BindView(R.id.id_swipe)
    PullRefreshLayout mSwipe;
    @BindView(R.id.id_multistateview)
    MultiStateView idMultistateview;
    private String dateStart = "", dateEnd = "", flag;
    private InvestRecordAdapter adapter;


    public static InvestRecordFragment newInstance(String flag) {
        InvestRecordFragment intance = new InvestRecordFragment();
        Bundle b = new Bundle();
        b.putString("flag", flag);
        intance.setArguments(b);
        return intance;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.content_universal_normal_recyclerview;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        flag = getArguments().getString("flag");
        getInvestRecord();
    }

    public void setDataAndNotify(String dateStart, String dateEnd) {
        command.setData(dateStart, dateEnd);
        adapter.getDataList().clear();
        adapter.notifyDataSetChanged();
        mDelegator.setDefault(true);
        mDelegator.beginHeaderRefreshing();
    }

    private void getInvestRecord() {
        GridLayoutManager layoutManager = new GridLayoutManager(mActivity, 2);
        GridSpacingItemDecoration decoration = new GridSpacingItemDecoration(2, WindowsUtils.dip2px(10), true);
        command = new InvestRecordCommand(dateStart, dateEnd, flag);
        adapter = new InvestRecordAdapter(mActivity, flag);
        mDelegator = new RecyclerViewDelegatorV2<>(mActivity, idMultistateview, mSwipe, mRecycler, adapter, command);
        mDelegator.setOnLoadFirstPageLister(this);
        mDelegator.setLayoutManager(layoutManager);
        mDelegator.addItemDecoration(decoration);
        mDelegator.beginHeaderRefreshing();

        mDelegator.setOnItemClickListener(new RecyclerViewDelegatorV2.OnItemClickListener<InvestRecordB>() {
            @Override
            public void OnItemClick(InvestRecordB item, int position) {
                if (MyViewsUtils.isFastDoubleClick()) return;
                if (MyViewsUtils.isFastDoubleClick()) return;
                String orderId = adapter.getDataList().get(position).getOrder_id();
                String simpleYearRate = adapter.getDataList().get(position).getSi_year_rate();
                String crowdfunding_id = adapter.getDataList().get(position).getCrowdfunding_id();
                String orderStatus = adapter.getDataList().get(position).getOrder_status();
                startActivity(InvestRecordDetailActivity.newInstance(mActivity, flag, orderId, simpleYearRate, crowdfunding_id, orderStatus));
            }
        });

    }


    @Override
    public void onLoad(CommandV2 mCommand) {
        ((InvestRecordCommand) mCommand).setData("", "");
    }
}
